<?php
class Models {

    private $_connection;

    public function __construct() {
        $this->_connection = DB::getDBConnection();
    }

    public function query($query) {
        mysql_query('SET NAMES utf8', $this->_connection);
        $ret = mysql_query($query, $this->_connection);
        if ($ret == null) {
            echo "MYSQL QUERY: $query\n";
            echo "MYSQL ERROR: ";
            print_r(mysql_error($this->_connection));
            echo "\n";
            die();
        }
        return $ret;
    }

    public function affectedRows() {
        $affectedRows = mysql_affected_rows($this->_connection);
        return $affectedRows;
    }

    public function getLastIDGenerated() {
        return mysql_insert_id();
    }

    public function fetchArray($query) {
        $ret = array();
        $result = $this->query($query);
        if ($result) {
            while ($row = mysql_fetch_assoc($result)) {
                $ret[] = $row;
            }
        }
        return $ret;
    }

    public function fetchRow($query) {
        $array = $this->fetchArray($query);
        if (empty($array)) {
            return null;
        }
        return $array[0];
    }

    public function escape($st) {
        return $this->escape($st);
    }
}